//
//  CommunityDetailViewController.m
//  FNer
//
//  Created by 王展 on 15/9/30.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "CommunityDetailViewController.h"
#import "AFNetworking.h"
#import "DetailHtworld.h"
#import "User.h"
#import "CommunityDetailTableViewCell.h"
#import "ChannelDetailViewController.h"
#import "DataHandle.h"
#import "MJRefresh.h"
#import "UserDetaiViewController.h"

@interface CommunityDetailViewController ()

@property(nonatomic,retain)NSMutableArray *htworldArray;//装社区详情的
@property(nonatomic,retain)UITableView *communityDetailTV;//社区列表
@property(nonatomic,assign)BOOL isUpLoading;//记录下拉上拉刷新
@property(nonatomic,retain)UILabel *titleLabel;


@end

@implementation CommunityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    self.titleLabel.text = self.achannel.channelName;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:18 * XWIDTH];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.titleLabel;
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.titleLabel.textColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];

    UIImage *rightImage = [UIImage imageNamed:@"频道详情icon.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
    
    [self setcommunityDetailTV];
    
    //加下拉刷新
    [self addHeaderRefresh];
}






//下拉刷新
-(void)addHeaderRefresh
{
    
        self.communityDetailTV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.isUpLoading = NO;
            [self getCommunityDetailData];
            
        }];
        [self.communityDetailTV.header beginRefreshing];
  }


-(void)leftButtonAction
{
  
    [self.navigationController popViewControllerAnimated:YES];
   // [SHAREDAPPDELE.myTB isHiddenTabBar:NO withViewController:self];
}

-(void)rightButtonAction
{
    NSLog(@"右侧按钮");
    ChannelDetailViewController *channelVC = [[ChannelDetailViewController alloc] init];
    [self.navigationController pushViewController:channelVC animated:YES];
    
    channelVC.aChannel = self.achannel;
    
}

//获取社区详情信息
-(void)getCommunityDetailData
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.imzhitu.com/operations/channel_queryChannelWorld"];
    
    NSString *string = [NSString stringWithFormat:@"%ld",self.achannel.id];
    NSDictionary *body = @{@"channelId":string,@"maxId":@"0",@"start":@"1",@"limit":@"10",@"trimExtras":@"false",@"commentLimit":@"2",@"likedLimit":@"10",@"completeLimit":@"10"};
    
    
    
    AFNetworkReachabilityManager *Netmanger = [AFNetworkReachabilityManager sharedManager];
   AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:body success:^(NSURLSessionDataTask *operation, id responseObject) {
        [Netmanger stopMonitoring];
        self.htworldArray = [NSMutableArray array];
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *bigArray = [bigDic objectForKey:@"htworld"];
        for (NSMutableDictionary *dic in bigArray) {
            DetailHtworld *aDetailHT = [[DetailHtworld alloc] init];
            [aDetailHT setValuesForKeysWithDictionary:dic];
            aDetailHT.aUser = [[BaseModel alloc] init];
            [aDetailHT.aUser setValuesForKeysWithDictionary:aDetailHT.userInfo];
            [self.htworldArray addObject:aDetailHT];
        }
        [self.communityDetailTV.header endRefreshing];
        [self.communityDetailTV reloadData];

    
    }failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"没有网络");
        self.htworldArray = [NSMutableArray array];
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPaht = [array lastObject];
        NSString *huancunDicFilePath = [documentPaht stringByAppendingPathComponent:@"CommunityDetailDic.plist"];
        NSMutableDictionary *bigDic = [NSKeyedUnarchiver unarchiveObjectWithFile:huancunDicFilePath];
        if (bigDic != nil) {
            NSMutableArray *bigArray = [bigDic objectForKey:@"htworld"];
            for (NSMutableDictionary *dic in bigArray) {
                DetailHtworld *aDetailHT = [[DetailHtworld alloc] init];
                [aDetailHT setValuesForKeysWithDictionary:dic];
                aDetailHT.aUser = [[BaseModel alloc] init];
                [aDetailHT.aUser setValuesForKeysWithDictionary:aDetailHT.userInfo];
                [self.htworldArray addObject:aDetailHT];
            }
            [self.communityDetailTV reloadData];
        }
        else
        {
            NSLog(@"没有缓存");
        }
        
        [self.communityDetailTV.header endRefreshing];
    }];
    
    

   }

//创建社区列表详情
-(void)setcommunityDetailTV
{
    self.communityDetailTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * XWIDTH, 667 * XHIGHT - 64) style:UITableViewStylePlain];
    self.communityDetailTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.communityDetailTV.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    [self.view addSubview:self.communityDetailTV];
    self.communityDetailTV.delegate = self;
    self.communityDetailTV.dataSource = self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.htworldArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CommunityDetailTableViewCell *detailCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (detailCell == nil) {
        detailCell = [[CommunityDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    detailCell.contentView.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    detailCell.aDetailHT = [self.htworldArray objectAtIndex:indexPath.row];
    
    return detailCell;
    
   
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0 * XHIGHT;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500 * XHIGHT;
}

//社区详情点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.communityDetailTV deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"row = %ld",indexPath.row);
    
    UserDetaiViewController *userDetailVC = [[UserDetaiViewController alloc] init];
    [self.navigationController pushViewController:userDetailVC animated:YES];
    
    userDetailVC.DetailHT = [self.htworldArray objectAtIndex:indexPath.row];
}

-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.titleLabel.textColor = [UIColor blackColor];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.titleLabel.textColor = [UIColor whiteColor];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    [SHAREDAPPDELE.myTB isHiddenTabBar:NO withViewController:self];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
